package com.lxu.util;

import com.lxu.entity.WordEntity;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 段落+表格占位+表格扩展行
 */
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringRunner.class)
public class WordComplex4 {
        /**
         * 表格字段填充+尾部扩展行
         */
        @Test
        public void fillComplexWord4Tail() throws Exception {
        Map<String, Object> textMap = generateTextMap();
        Map<String, List<List<String>>> tableMap = generateTableMap();
        int rows = tableMap.get("table1").size();
        WordEntity wordEntity = new WordEntity();
        wordEntity.setTextMap(textMap);

        wordEntity.setStartRow(2);
        wordEntity.setRows(rows);
        /**
         * 填充占位符
         */
        InputStream in = this.getClass().getResourceAsStream("/templates/担保业务初审批量立项表（金融机构）表头.docx");
        XWPFDocument xwpfDocument = XWPFUtils.fillComplexWord(in, wordEntity);
        XWPFUtils.save("D:\\ComplexWord.docx", xwpfDocument);

        /**
         * 中间扩展表格行数
         */

            wordEntity.setTableMap(tableMap);

        InputStream in2 = new FileInputStream(new File("D:\\ComplexWord.docx"));
        XWPFDocument xwpfDocument2 = new XWPFDocument(in2);
        XWPFTable table = XWPFUtils.getTableByIndex(xwpfDocument2, 1);
        XWPFUtils.insertRows(table, wordEntity.getStartRow(), rows - 3);
        XWPFUtils.save("D:\\ComplexWord.docx", xwpfDocument2);

        /**
         * 填充表格数据
         */
        InputStream in3 = new FileInputStream(new File("D:\\ComplexWord.docx"));
        XWPFDocument xwpfDocument3 = XWPFUtils.fillComplexWord(in3, wordEntity);
        XWPFUtils.save("D:\\ComplexWord.docx", xwpfDocument3);
    }

        private Map<String, List<List<String>>> generateTableMap() {
        List<List<String>> rows = new ArrayList<List<String>>();
        Map<String, List<List<String>>> map = new HashMap<String, List<List<String>>>();
        for (int j = 0; j < 8; j++) {
            List<String> cols = new ArrayList<String>();
            cols.add("经营种类" + j);
            cols.add("经营品种" + j);
            cols.add("经营方式" + j);
            cols.add("经营时长" + j);
            cols.add("经营规模" + j);
            cols.add("生产周期" + j);
            cols.add("单位产量" + j);
            cols.add("总产量" + j);
            cols.add("产值" + j);
            cols.add("单位成本" + j);
            cols.add("总成本" + j);
            cols.add("净利润总额" + j);
            cols.add("总额" + j);
            rows.add(cols);
        }
        map.put("table1", rows);
        return map;
    }

    private Map<String, Object> generateTextMap() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("no", "001");
        map.put("date", "2021-09-01");
        map.put("productName", "农担公司金融产品");
        map.put("orgName", "中国建设银行");
        map.put("govName", "合肥市人民政府");
        map.put("company", "天勤盛创");
        map.put("myRate", "50");
        map.put("finaceRate", "40");
        map.put("otherRate", "10");
        map.put("mScope", "1-100");
        map.put("customerType", "自然人");
        map.put("sxType", "不知道");
        map.put("contractType", "字数比较多，吱吱吱吱吱吱吱吱吱吱吱吱，多打点字啊。。。。。。。。。。。。。。");
        map.put("riskDesc", "风险风险风险风险风险风险风险风险");
        map.put("bank", "中国建设银行");
        map.put("area", "合肥市");
        map.put("totalP", "10");
        map.put("totalM", "1000");
        map.put("agent", "合肥市长");
        map.put("detailOrg", "安徽省天勤盛创股份有限公司");
        return map;
    }
}
